export enum productTypeEnum {
  Nomal,
  Aged,
  Sulfuras,
  Backstage,
}

export default class Product{
  private type: number;
  private sellIn: number;
  private quality: number;
  constructor(type: number, sellIn: number, quality: number) {
    if(quality > 50 || quality < 0) {
      throw new RangeError
    }
    this.type = type
    this.sellIn = sellIn;
    this.quality = quality;
  }
  public getSellIn() {
    return this.sellIn
  }
  public getQuality() {
    return this.quality
  }
  private setQuality(quality:number) {
    this.quality = quality <= 0 ? 0 : quality > 50 ? 50 : quality
  }
  
  private increaseQuality (num: number) {
    if(num === 0){
      this.setQuality(0)
    } else {
      this.setQuality(this.quality + num)
    }
  }
  private decreaseSellIn () {
    this.sellIn = this.sellIn - 1 
  }

  private nomalCount () {
    let { sellIn, quality } = this
    quality -= sellIn > 0 ? 1 : 2
    this.setQuality(quality)
  }

  private agedCount() {
    let {sellIn, quality } = this
    quality += sellIn > 0 ? 1 : 2
    this.setQuality(quality)
  }
  private backStageCount() {
    let { sellIn } = this
    let quality = sellIn > 10 ? 1 : sellIn > 5 ? 2 : sellIn > 0 ? 3 : 0
    this.increaseQuality(quality)
  }
  
  public updateByOneDay () {
    const { type } = this
    if (type !== productTypeEnum.Sulfuras) {
      this.decreaseSellIn()
    }
    switch (type) {
      case productTypeEnum.Nomal: {
        this.nomalCount()
        break;
      }
      case productTypeEnum.Aged: {
        this.agedCount();
        break;
      }
      case productTypeEnum.Backstage: {
        this.backStageCount()
      }
      default: {}
    }
  }
  
  
}

